package com.go.where.manager.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.go.where.manager.common.Constants;
import com.go.where.manager.dao.ActivityDao;
import com.go.where.manager.dao.BigClassDao;
import com.go.where.manager.dao.SmallClassDao;
import com.go.where.manager.page.Pagination;
import com.go.where.manager.page.BaseQuery.OrderType;
import com.go.where.manager.pojo.ActivityInfo;
import com.go.where.manager.pojo.BigClassInfo;
import com.go.where.manager.pojo.SmallClassInfo;
import com.go.where.manager.pojo.user.UserInfoVO;
import com.go.where.manager.service.BigClassService;

@Service("bigClassService")
public class BigClassServiceImpl implements BigClassService {

	@Resource(name = "bigClassDao")
	private BigClassDao bigClassDao;
	@Resource
	private SmallClassDao smallClassDao;
	@Resource
	private ActivityDao activityDao;

	@Override
	public BigClassInfo queryByPrimartKey(String cid) {
		return bigClassDao.getByKey(cid);
	}

	@Override
	public Pagination<BigClassInfo> queryPage(BigClassInfo bigClassInfo) {
		if (StringUtils.isNotEmpty(bigClassInfo.getOrderBy())
				&& bigClassInfo.getOrderType() != null) {
			if (bigClassInfo.getOrderType() == OrderType.asc) {
				bigClassInfo.setPage_orderByAsc(bigClassInfo.getOrderBy());

			} else {
				bigClassInfo.setPage_orderByDesc(bigClassInfo.getOrderBy());
			}
		}
		Integer count = bigClassDao.totalCount(bigClassInfo);
		bigClassInfo.calculate();
		List<BigClassInfo> list = bigClassDao.queryList(bigClassInfo);
		Pagination<BigClassInfo> page = bigClassDao.queryPagination(count,
				list, bigClassInfo);
		return page;
	}

	@Override
	public int save(BigClassInfo bigClassInfo, UserInfoVO currentUser) {
		bigClassInfo.setPublishTime(new Date());
		bigClassInfo.setUserId(currentUser.getUserId());
		bigClassInfo.setIsDelete(Constants.NOT_DELETED);
		return bigClassDao.insert(bigClassInfo);
	}

	@Override
	public int update(BigClassInfo bigClassInfo, UserInfoVO currentUser) {
		bigClassInfo.setUpdateTime(new Date());
		bigClassInfo.setUserId(currentUser.getUserId());
		SmallClassInfo sc = new SmallClassInfo();
		sc.setBigClass(bigClassInfo.getClassId());
		sc.setIsDelete(Constants.DELETED);
		sc.setUpdateTime(new Date());
		sc.setUserId(currentUser.getUserId());
		ActivityInfo ai = new ActivityInfo();
		ai.setBigClassId(bigClassInfo.getClassId());
		ai.setIsDelete(Constants.DELETED);
		ai.setUpdateTime(new Date());
		ai.setUserId(currentUser.getUserId());
		smallClassDao.update(sc);
		activityDao.updateByClassId(ai);
		return bigClassDao.update(bigClassInfo);
	}

	@Override
	public List<BigClassInfo> queryAll(BigClassInfo bigInfo) {
		return bigClassDao.queryAll(bigInfo);
	}

}
